package com.android.pdd.baseapp.hook;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;

public class EditTextCallbackDialog extends BaseDialog {
    TextView titleTv;
    EditText editText;
    TextView btnCancel;
    TextView btnOk;

    public EditTextCallbackDialog(Context context) {
        super(context);
    }

    @Override
    protected View provideView() {
        LinearLayout linearLayout =
                ViewCreater.LinearLayoutBuilder.newInstance(context)
                        .setOrientationVertical()
                        .setWidthMatch()
                        .build();
        GradientDrawable gd = SelectorUtil.buildGradientDrawable(0xffffffff, dp2px(10));
        linearLayout.setBackground(gd);
        linearLayout.setPadding(dp2px(15), dp2px(25), dp2px(15), dp2px(25));

        titleTv = ViewCreater.TextViewBuilder.newInstance(context)
                .setTextColor(0xff222222)
                .setTextSize(16)
//                .setGravity(Gravity.CENTER_HORIZONTAL)
                .build();
        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        params.bottomMargin = dp2px(12);
        titleTv.setLayoutParams(params);
        linearLayout.addView(titleTv);

        ScrollView scrollView = new ScrollView(context);
        scrollView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        editText = new EditText(context);
        params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        editText.setLayoutParams(params);
        editText.setMaxHeight(dp2px(300));
        scrollView.addView(editText);
        linearLayout.addView(scrollView);


        LinearLayout layoutChild = ViewCreater.LinearLayoutBuilder.newInstance(context)
                .setOrientationHorizontal()
                .setMargin(0, dp2px(6), 0, 0)
                .setWidthMatch()
                .build();
        btnCancel = new ViewCreater.TextViewBuilder(context)
                .setText("取消")
                .setGravity(Gravity.CENTER)
                .setLayoutWeight(1)
                .setSelector(0xff6E7B8B, 0xff528B8B, dp2px(4))
                .setPadding(dp2px(12), dp2px(3), dp2px(12), dp2px(3))
                .setTextSize(16)
                .setTextColor(0xffffffff)
                .setHeight(dp2px(40))
                .build();
        btnOk = new ViewCreater.TextViewBuilder(context)
                .setGravity(Gravity.CENTER)
                .setText("确认")
                .setSelector(0xff6E7B8B, 0xff528B8B, dp2px(4))
                .setLayoutWeight(1)
                .setHeight(dp2px(40))
                .setMargin(dp2px(20), 0, 0, 0)
                .setPadding(dp2px(12), dp2px(3), dp2px(12), dp2px(3))
                .setTextSize(16)
                .setTextColor(0xffffffff)
                .build();
        layoutChild.addView(btnCancel);
        layoutChild.addView(btnOk);
        linearLayout.addView(layoutChild);
        return linearLayout;
    }

    @Override
    protected void initView() {
        if (!TextUtils.isEmpty(hint)) {
            editText.setHint(hint);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        } else {
            titleTv.setVisibility(View.GONE);
        }
        btnCancel.setOnClickListener(view -> {
            dismiss();
        });
        btnOk.setOnClickListener(view -> {
            String input = editText.getText().toString();
            if (TextUtils.isEmpty(input)) {
                ToastUtils.showLong("输入为空");
                return;
            }
            if (callback != null) {
                callback.onInput(input, EditTextCallbackDialog.this);
            }
            dismiss();
        });
    }

    @Override
    protected void initData() {
        setWidth(ScreenUtils.getScreenWidth() * 4 / 5);
    }

    public static EditTextCallbackDialog newInstance(Activity activity, Callback callback) {
        EditTextCallbackDialog dialog = new EditTextCallbackDialog(activity);
        dialog.callback = callback;
        return dialog;
    }

    private Callback callback;

    public interface Callback {
        void onInput(String text, Dialog dialog);
    }

    private String hint;
    private String title;

    public EditTextCallbackDialog hint(String hint) {
        this.hint = hint;
        return this;
    }

    public EditTextCallbackDialog title(String title) {
        this.title = title;
        return this;
    }
}
